const mongoose = require('mongoose')

//connect
mongoose.connect("mongodb://127.0.0.1:27017/test",{
    useNewUrlParser : true,
    useUnifiedTopology : true
});
const con = mongoose.connection
con.on('error', console.error.bind(console, '连接数据库失败'));
con.once('open',()=>{
    //schema 字段约束
    const user_Schema = new mongoose.Schema({
        username:{
            type:String,
            requried: true,
            min:4
        },
        password:{
            type:String,
            required:true
        },
        gender:{
            type:Number,
            required:true
        },
        mobile:{
            type:String,
            required:true
        }
    })

    //model
    //options: 模型名，schema ， 真正的表名（可以不写，默认为模型名s）
    const model = mongoose.model("user",user_Schema)//表名users

    model.insertMany([{
        username:'AEI',
        password:'abc',
        gender:0,
        mobile:12345
    }],function(err) {
        if(err)
        console.log('插入出错:'+err)
    })

    console.log(model.find({username:'AEI'},(err,docs) => {
        if(!err){
             console.log(docs)
         }
        else {
            console.log(err)
        }
     }))
})

